﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SomeGame.GUI.Widgets.InlineWidgets {

    public class Textbox : TextWidget {

        private const double CursorBlinkTimer = 0.5;
        private double CursorBlinkAccumulatedSeconds;
        private bool CursorBlinkShowing = true;
        public int MaxLength;
        private Vector2 TextBounds;
        private Vector2 TextLeftCenter;
        private Vector2 TextboxLeftCenter;

        public Textbox() {
            // ...
        }

        public Textbox(Texture2D texture, Color color) : base(texture, color) {
            // ...
        }

        public Textbox(Texture2D texture, int width, int height, Color color) : base(texture, width, height, color) {
            // ...
        }

        public Textbox(Texture2D texture, Color color, SpriteFont font, Color fontColor)
            : base(texture, texture.Width, texture.Height, color, font, fontColor, "") {
            // ...
        }

        public Textbox(Texture2D texture, int width, int height, Color color, SpriteFont font, Color fontColor)
            : base(texture, width, height, color, font, fontColor, "") {
            // ...
        }

        public override void Update(GameTime gameTime) {
            // Mouse events should only work on the screen 'closest' to the player.
            if (!Screen.IsTopScreen) {
                return;
            }

            // Click.
            if (GameEngine.MouseState.LeftButton == ButtonState.Pressed &&
                GameEngine.PreviousMouseState.LeftButton == ButtonState.Released) {
                HasFocus = Bounds.Contains(GameEngine.MousePoint);
            }

            if (HasFocus) {
                foreach (char c in GameEngine.WindowsCharsEntered) {
                    if (!GameEngine.PreviousWindowsCharsEntered.Contains(c)) {
                        if (c == 8) {
                            if (Text.Length > 0) {
                                Text = Text.Remove(Text.Length-1);
                            }
                        } else if (MaxLength == 0 || Text.Length < MaxLength) {
                            Text += c;
                        }
                    }
                }
                TextboxLeftCenter = new Vector2(Bounds.X+Padding.Left, Bounds.Y+Bounds.Height/2f);
                TextBounds = GameEngine.Consolas16.MeasureString(Text);
                TextLeftCenter = new Vector2(0, GameEngine.Consolas16.LineSpacing/2f);

                CursorBlinkAccumulatedSeconds += gameTime.ElapsedGameTime.TotalSeconds;
                if (CursorBlinkAccumulatedSeconds > CursorBlinkTimer) {
                    CursorBlinkShowing = !CursorBlinkShowing;
                    CursorBlinkAccumulatedSeconds -= CursorBlinkTimer;
                }
            }
        }

        public override void Draw() {
            base.Draw();
            if (!String.IsNullOrWhiteSpace(Text)) {
                GameEngine.SpriteBatch.DrawString(GameEngine.Consolas16,
                    Text,
                    TextboxLeftCenter,
                    FontColor,
                    0,
                    TextLeftCenter,
                    1.0f,
                    SpriteEffects.None,
                    0f);
            }
            if (HasFocus && (MaxLength == 0 || Text.Length != MaxLength) && CursorBlinkShowing) {
                GameEngine.SpriteBatch.DrawString(GameEngine.Consolas16,
                    "|",
                    new Vector2(TextboxLeftCenter.X+TextBounds.X, TextboxLeftCenter.Y),
                    FontColor,
                    0,
                    TextLeftCenter,
                    1.0f,
                    SpriteEffects.None,
                    0f);
            }
        }

    }

}